<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Register extends Public_Controller {

    public $error = '';
    public $message = '';
    public $data;
    public $aUser;

    public function __construct() {
        parent::__construct();
        $this->_load();
    }

    public function index() {
        $a = $this->load->view('register', NULL, true);
        $b = str_replace("{phrase var='general.host'}", "http://localhost/store/application", $a);
        echo $b;
    }

    private function _register() {
        $aConfig = $this->_getRuleRegister();
        $this->validation->set_rules($aConfig);
        if ($this->validation->run() == FALSE) {
            $this->error = true;
            return false;
        }

        unset($this->aUser['submit']);
        unset($this->aUser['re_password']);
        if ((bool) $oUser = $this->user->register($this->aUser)) {
            $this->sendMail($oUser);
        }
        $this->message = 'Register success';
    }

    private function _getRuleRegister() {
        return array(
            array(
                'field' => 'user[email]',
                'label' => 'Email',
                'rules' => 'trim|required|valid_email'
            ),
            array(
                'field' => 'user[fullname]',
                'label' => 'Fullname',
                'rules' => 'trim|required|min_length[5]|max_length[12]|xss_clean'
            ),
            array(
                'field' => 'user[password]',
                'label' => 'Password',
                'rules' => 'trim|required|min_length[5]|max_length[12]|matches[user[re_password]]|alpha_numeric'
            ),
            array(
                'field' => 'user[re_password]',
                'label' => 'Re-password',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'user[address]',
                'label' => 'Address',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'user[birthday]',
                'label' => 'Birthday',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'user[sex]',
                'label' => 'Sex',
                'rules' => 'trim|required|is_natural_no_zero'
            )
        );
    }

    public function sendMail($oUser = null) {
        $sConfirmedLink = 'http://' . $this->input->server('SERVER_NAME') . $this->input->server('SCRIPT_NAME')
                . '/profile/confirmAccount/' . $oUser->sCodeConfirmEmail;
        $sBodyMail = "<a href='{$sConfirmedLink}'>{$sConfirmedLink}</a>";

        $aConfig = array(
            'protocol' => 'smtp',
            'smtp_host' => 'lampart.com.vn',
            'smtp_port' => '25'
        );

        $aConfig['mailtype'] = "html";
        $this->load->library('email', $aConfig);
        $this->email->set_newline("\r\n");

        $this->email->from('vantai@lampart.com.vn', 'Book store');
        $this->email->to('millionairetai@yahoo.com.vn');
        $this->email->subject('Email active account');
        $this->email->message($sBodyMail);

        if ($this->email->send()) {
            echo 'Your email was sent, fool.';
        } else {
            show_error($this->email->print_debugger());
        }
    }

    public function confirmAccount() {
        $sCodeConfirm = $this->uri->segment(3);
        if (empty($sCodeConfirm)) {
            echo 'Active account unsuccessful.';
            return false;
        }
    }

    public function load() {
        $this->load->model('user');
        $this->load->library('form_validation', NULL, 'validation');
        $this->error = '';
        $this->message = '';
        $this->data = array();
        $this->aUser = array();
    }

    public function finalize() {
        $this->data['error'] = $this->error;
        $this->data['message'] = $this->message;
        $this->data['user'] = $this->data['user'] = (object) $this->input->post();
    }

    private function _load() {
        $this->load->helper(array('url', 'form'));
        $this->load->model('user');
    }

    private function _replacePhrase() {
        $this->lang->load('admin/general', $this->_slang_name);
        $this->lang->load('admin/login', $this->_slang_name);
        $this->fill_phrase('login', array('sloginFail' => $this->_serror), $this->lang->language);
    }
}
?>
